package com.liwenfeng.iostream;

import com.liwenfeng.Pi;
import jakarta.annotation.security.DeclareRoles;
import org.apache.commons.io.FileUtils;

import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.io.*;
import java.security.Signature;
import java.sql.Driver;
import java.util.Properties;
import java.util.Set;

public class BufferStreamTest {
    public void read(String source) {
        try (
                FileReader fr = new FileReader(source);
                BufferedReader br = new BufferedReader(fr);
        ) {
            int read;
            while ((read = br.read()) != -1) {
                System.out.print((char) read);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void readLine(String source){
        try (
                FileReader fr = new FileReader(source);
                BufferedReader br = new BufferedReader(fr);
        ) {
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void write(String target, String content) {
        try (
                FileWriter fw = new FileWriter(target,true);
                BufferedWriter bw = new BufferedWriter(fw);
        ) {
            bw.write(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void writeLine(String target, String content) {
        try (
                FileWriter fw = new FileWriter(target,true);
                BufferedWriter bw = new BufferedWriter(fw);
        ) {
            bw.write(content);
            bw.newLine();
            bw.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        //输出重定向，把输出到控制台的信息改为输出到文件中
//        System.setOut(new PrintStream("D:/code/java/JAVA22/function2framework/src/main/java/com/liwenfeng/test1.txt"));
//        System.out.println("输出成功。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");

//        String fileName = "D:/code/java/JAVA22/function2framework/src/main/java/com/liwenfeng/test.txt";
//        BufferStreamTest test = new BufferStreamTest();
//        test.write(fileName,"床前明月光；\r\n");
//        test.write(fileName,"疑是地上霜；\r\n");
//        test.write(fileName,"举头望明月；\r\n");
//        test.write(fileName,"低头思故乡。\r\n");
//        test.writeLine(fileName,"桃花潭水深千尺；");
//        test.writeLine(fileName,"不及汪伦送我情。");
//        test.readLine(fileName);

        //IO框架
        //下载commons-io.jar文件，在src目录下新建目录，把jar文件拷入，右键新键的目录，选“添加为库...”
//        FileUtils.copyFile(new File("D:/code/java/JAVA22/function2framework/src/main/java/com/liwenfeng/test.txt"),
//                new File("D:/code/java/JAVA22/function2framework/src/main/java/com/liwenfeng/test2.txt"));

        //读取属性文件
//        Properties properties = new Properties();
//        properties.load(Pi.class.getClassLoader().getResourceAsStream("application.properties"));
//        Set<String> strings = properties.stringPropertyNames();
//        for (String string : strings) {
//            System.out.println(string+"=="+properties.getProperty(string));
//        }
//        //修改属性值
//        properties.setProperty("server.port","80");
//        //修改后的值保存到文件中
//        properties.store(new FileWriter("src/main/resources/application.properties"),null);

    }
}
